package com.ffmpeg.util.infra.config;

import java.util.HashMap;
import java.util.Map;

import com.ffmpeg.util.infra.constant.DealWhenExistEnum;
import com.ffmpeg.util.infra.handle.file.IDealWhenExistHandle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author :wm
 * @description : 初始化
 */
@Component
public class InitConfig implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    private Map<DealWhenExistEnum, IDealWhenExistHandle> dealWhenExistHandleMap = new HashMap<>(16);

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        InitConfig.applicationContext = applicationContext;
        this.initDealWhenExistHandleMap(applicationContext);
    }

    private void initDealWhenExistHandleMap(ApplicationContext applicationContext){
        applicationContext.getBeansOfType(IDealWhenExistHandle.class)
                .forEach((beanName, beanInstance) ->
                        dealWhenExistHandleMap.put(beanInstance.getType(), beanInstance)
                );
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public Map<DealWhenExistEnum, IDealWhenExistHandle> getDealWhenExistHandleMap() {
        return dealWhenExistHandleMap;
    }

    public InitConfig setDealWhenExistHandleMap(Map<DealWhenExistEnum, IDealWhenExistHandle> dealWhenExistHandleMap) {
        this.dealWhenExistHandleMap = dealWhenExistHandleMap;
        return this;
    }
}
